package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.AdvertisingInfoBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.AdvertisingInfoMapper;
import com.molichuxing.services.infrastructure.dao.AdvertisingStoreMapper;
import com.molichuxing.services.infrastructure.dto.request.create.AdvertisingInfoCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.AdvertisingInfoModifyDto;
import com.molichuxing.services.infrastructure.dto.response.AdvertisingInfoDto;
import com.molichuxing.services.infrastructure.service.AdvertisingInfoService;
import com.molichuxing.services.property.AdvertisingInfoMediaEnum;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 11:05 2019/10/30
 * @Modified By:
 **/
@Service("advertisingInfoService")
public class AdvertisingInfoServiceImpl implements AdvertisingInfoService {

    @Resource
    private AdvertisingInfoMapper advertisingInfoMapper;
    
    @Resource
	private AdvertisingStoreMapper advertisingStoreMapper;

    /**
     * 根据门店id查询广告信息
     * @param storeId
     * @return
     */
    @Override
    public List<AdvertisingInfoDto> getAdvertisingInfoDto(Integer storeId) {
    	Integer advertId = advertisingStoreMapper.getAdvertIdByStoreId(storeId);
    	List<AdvertisingInfoBean> beanList = advertisingInfoMapper.getAdvertisingInfoList(advertId);
        return Convert.toAdvertisingInfoDto(beanList);
    }
    
    /**
     * 根据广告id和标识 查询广告信息
     * @param storeId
     * @param media
     * @return
     */
    @Override
    public List<AdvertisingInfoDto> getAdvertisingInfoDtoByAdvertId(Integer advertId,String media) {
    	List<AdvertisingInfoBean> beanList = advertisingInfoMapper.getAdvertisingInfoByMedia(advertId,media);
        return Convert.toAdvertisingInfoDto(beanList);
    }
    
    /**
     * 新增素材信息
     * @param createDto
     * @return
     */
    @Override
    public Boolean create(AdvertisingInfoCreateDto createDto) {
    	AdvertisingInfoBean bean = new AdvertisingInfoBean();
    	BeanUtils.copyProperties(createDto, bean);
    	int num = advertisingInfoMapper.create(bean);
    	return num==1;
    }
    
    
    /**
     * 修改素材信息
     * @param modifyDto
     * @return
     */
    @Override
    public Boolean modify(AdvertisingInfoModifyDto modifyDto) {
    	Integer advertId = modifyDto.getAdvertId();
    	//逻辑删除
    	advertisingInfoMapper.deleteByAdvertId(advertId);
    	List<String> imgList = modifyDto.getImgList();
		if(null != imgList && !imgList.isEmpty()) {
			for(int i=0; i<imgList.size() ;i++) {
				int sequence =i+1;
				create(sequence,advertId,imgList.get(i),AdvertisingInfoMediaEnum.IMG.getValue());
			}
		}
		List<String> videoList = modifyDto.getVideoList();
		if(null != videoList && !videoList.isEmpty()) {
			for(int i=0; i<videoList.size() ;i++) {
				int sequence =i+1;
				create(sequence,advertId,videoList.get(i),AdvertisingInfoMediaEnum.VIDEO.getValue());
			}
		}
    	return true;
    }
    
    /**
	 * 保存素材接口
	 * @param sequence
	 * @param advertId
	 * @param url
	 * @param media
	 * @return
	 */
	private Boolean create(Integer sequence,Integer advertId,String url,String media) {
		AdvertisingInfoBean bean = new AdvertisingInfoBean();
		bean.setSequence(sequence);
		bean.setAdvertId(advertId);
		bean.setUrl(url);
		bean.setIsDelete((short)0);
		bean.setForceUpdate((short)1);
		bean.setMd5Value("md5");
		bean.setMedia(media);
		int num = advertisingInfoMapper.create(bean);
		return num == 1;
	}
}
